//
//  LGPrescriptionChartControllerViewController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2025/1/3.
//

#import "LGPrescriptionChartController.h"
#import "LGPrescriptionChartView.h"
#import "LGChartDetailAlertView.h"
@interface LGPrescriptionChartController ()<LGPrescriptionChartDelegate,UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)LGPrescriptionChartView *chartView1;
/**   */
@property (nonatomic , strong)LGPrescriptionChartView *chartView2;
/**   */
@property (nonatomic , strong)LGPrescriptionChartView *chartView3;
/**   */
@property (nonatomic , strong)LGPrescriptionChartView *chartView4;
/**   */
@property (nonatomic , strong)LGChartDetailAlertView *alertView;
@end

@implementation LGPrescriptionChartController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
    [self requestChartData1];
    [self requestChartData2];
    [self requestChartData3];
    [self requestChartData4];
}

//次数-吸气、呼气、排痰
-(void)requestChartData1{
    [RequestUtil withGET:@"/breathTrainRecords/getBreatheNum" parameters:@{@"patientId":self.patientid,@"trainType":self.type} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.chartView1.dataArray = returnData;
        }else{
            self.chartView1.dataArray = @[];
        }
    } error:^(NSError *error) {
    
    }];
}
//最大流量-吸气、呼气
-(void)requestChartData2{
    NSString *action = [self.type isEqualToString:@"2"]?@"/breathTrainRecords/getBreatheTime":@"/breathTrainRecords/getBreatheMaxFlow";
    [RequestUtil withGET:action parameters:@{@"patientId":self.patientid,@"trainType":self.type} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.chartView2.dataArray = returnData;
        }else{
            self.chartView2.dataArray = @[];
        }
    } error:^(NSError *error) {
    
    }];
    
}
//最大肌力 - 吸气、呼气、排痰(压力)
-(void)requestChartData3{
    [RequestUtil withGET:@"/breathTrainRecords/getBreatheMaxPressure" parameters:@{@"patientId":self.patientid,@"trainType":self.type} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.chartView3.dataArray = returnData;
        }else{
            self.chartView3.dataArray = @[];
        }
    } error:^(NSError *error) {
    
    }];
    
}
//平均容积-吸气、呼气 （平均振幅-排痰）
-(void)requestChartData4{
    NSString *action = [self.type isEqualToString:@"2"]?@"/breathTrainRecords/getBreatheAvgFreq":@"/breathTrainRecords/getBreatheAvgVol";
    [RequestUtil withGET:action parameters:@{@"patientId":self.patientid,@"trainType":self.type} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.chartView4.dataArray = returnData;
        }else{
            self.chartView4.dataArray = @[];
        }
    } error:^(NSError *error) {
    
    }];
}


-(void)showAlertView:(NSArray *)dataArray date:(NSString *)dateString point:(CGPoint)point{
    CGFloat marginX = (point.x>Screen_W/2.0)?(point.x-viewPix(140)-3):(point.x+3);
    self.alertView.frame = CGRectMake(marginX, point.y+3, viewPix(140), viewPix(35)+viewPix(24)*dataArray.count);
    self.alertView.dateString = dateString;
    self.alertView.dataArray = dataArray;
    self.alertView.hidden = NO;
    if(point.x>Screen_W/2.0){
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.alertView.bounds byRoundingCorners:UIRectCornerTopLeft | UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
        self.alertView.cornerLayer.path = path.CGPath;
    }else{
        UIBezierPath *path = [UIBezierPath bezierPathWithRoundedRect:self.alertView.bounds byRoundingCorners: UIRectCornerTopRight|UIRectCornerBottomLeft|UIRectCornerBottomRight cornerRadii:CGSizeMake(15, 15)];
        self.alertView.cornerLayer.path = path.CGPath;
    }
}

-(void)hiddenAlertView{
    self.alertView.hidden = YES;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.alertView.hidden = YES;
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.title = @"历史处方详情";
    self.view.backgroundColor = [UIColor colorWithString:@"#F3F1FC"];
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.chartView1];
    [self.scrollView addSubview:self.chartView2];
    [self.scrollView addSubview:self.chartView3];
    [self.scrollView addSubview:self.chartView4];
    [self.scrollView addSubview:self.alertView];
    self.scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.chartView4.frame)+viewPix(50));
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight-viewPix(44))];
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(LGPrescriptionChartView *)chartView1{
    if(!_chartView1){
        _chartView1 = [[LGPrescriptionChartView alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(220))];
        [_chartView1 setShadoWithColor:@"#E3E0EF" offset:CGSizeMake(0, 0) opacity:0.1 radius:20];
        _chartView1.backgroundColor = [UIColor whiteColor];
        _chartView1.delegate = self;
        _chartView1.type_count = YES;
        if ([self.type isEqualToString:@"0"]) {
            _chartView1.titleLabel.text = @"呼气次数";
            _chartView1.unit = @"次";
            
        }else if ([self.type isEqualToString:@"1"]){
            _chartView1.titleLabel.text = @"吸气次数";
            _chartView1.unit = @"次";
            
        }else{
            _chartView1.titleLabel.text = @"排痰呼气次数";
            _chartView1.unit = @"次";
        }
    }
    return _chartView1;
}

-(LGPrescriptionChartView *)chartView2{
    if(!_chartView2){
        _chartView2 = [[LGPrescriptionChartView alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.chartView1.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(220))];
        [_chartView2 setShadoWithColor:@"#E3E0EF" offset:CGSizeMake(0, 0) opacity:0.1 radius:20];
        _chartView2.backgroundColor = [UIColor whiteColor];
        _chartView2.delegate = self;
        if ([self.type isEqualToString:@"0"]) {
            _chartView2.titleLabel.text = @"最大呼气流量(ml/s)";
            _chartView2.unit = @"ml/s";
            
        }else if ([self.type isEqualToString:@"1"]){
            _chartView2.titleLabel.text = @"最大吸气流量(ml/s)";
            _chartView2.unit = @"ml/s";
            
        }else{
            _chartView2.titleLabel.text = @"呼气时间(s)";
            _chartView2.unit = @"s";
        }
    }
    return _chartView2;
}

-(LGPrescriptionChartView *)chartView3{
    if(!_chartView3){
        _chartView3 = [[LGPrescriptionChartView alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.chartView2.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(220))];
        [_chartView3 setShadoWithColor:@"#E3E0EF" offset:CGSizeMake(0, 0) opacity:0.1 radius:20];
        _chartView3.backgroundColor = [UIColor whiteColor];
        _chartView3.delegate = self;
        if ([self.type isEqualToString:@"0"]) {
            _chartView3.titleLabel.text = @"最大呼气肌力(cmH2O)";
            _chartView3.unit = @"cmH2O";
            
        }else if ([self.type isEqualToString:@"1"]){
            _chartView3.titleLabel.text = @"最大吸气肌力(cmH2O)";
            _chartView3.unit = @"cmH2O";
            
        }else{
            _chartView3.titleLabel.text = @"最大压力(cmH2O)";
            _chartView3.unit = @"cmH2O";
        }
    }
    return _chartView3;
}

-(LGPrescriptionChartView *)chartView4{
    if(!_chartView4){
        _chartView4 = [[LGPrescriptionChartView alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.chartView3.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(220))];
        [_chartView4 setShadoWithColor:@"#E3E0EF" offset:CGSizeMake(0, 0) opacity:0.1 radius:20];
        _chartView4.backgroundColor = [UIColor whiteColor];
        _chartView4.delegate = self;
        if ([self.type isEqualToString:@"0"]) {
            _chartView4.titleLabel.text = @"平均呼气容积(ml)";
            _chartView4.unit = @"ml";
            
        }else if ([self.type isEqualToString:@"1"]){
            _chartView4.titleLabel.text = @"平均吸气容积(ml)";
            _chartView4.unit = @"ml";
            
        }else{
            _chartView4.titleLabel.text = @"震动频率(Hz)";
            _chartView4.unit = @"Hz";
        }
    }
    return _chartView4;
}

-(LGChartDetailAlertView *)alertView{
    if(!_alertView){
        _alertView = [[LGChartDetailAlertView alloc] initWithFrame:CGRectMake(0, 0, viewPix(140), viewPix(50))];
        _alertView.hidden = YES;
    }
    return _alertView;
}

@end
